﻿/// <reference path="../../../lib/bootstrap3-dialog/dist/js/bootstrap-dialog.min.js" />

//组合树框
function ComboTree(ctrlTree, ctrlValue, ctrlText,ctrlBtn, ctrlDropdown, idKey, pIdKey,nodes,canSelectType,canNotSelectMessage)
{
    var trees;
    var setting = {
        data: {
            simpleData: {
                enable: true,
                idKey: idKey,
                pIdKey: pIdKey,
                rootPId: 0
            }
        },
        callback: {
            onClick: function (event, treeId, treeNode) {
                if (canSelectType != undefined && canSelectType > 0 && treeNode.type == canSelectType) {
                    $(ctrlValue).val(treeNode.id);
                    $(ctrlText).text(treeNode.name);
                    $(ctrlDropdown).hide();
                }
                else BootstrapDialog.alert({
                    title: '消息',
                    message: canNotSelectMessage,
                    type: BootstrapDialog.TYPE_WARNING,
                    closable: true,
                    draggable: true,
                    buttonLabel: '确定'
                });
            }
        }
    };
    trees = $.fn.zTree.init($(ctrlTree), setting, nodes);
    trees.expandAll(true);
    //选中节点
    var node = trees.getNodeByParam(idKey, $(ctrlValue).val(), null);
    if (node != null) {
        trees.selectNode(node);
        
        $(ctrlValue).text(node.id);
        $(ctrlText).text(node.name);
    }
    $(ctrlBtn).click(function () { $(ctrlDropdown).show() });
    return trees;
}